package com.jbp.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256Util {

    public static String getSHA256(String toHash){
        try {
            // 假设这是你要拼接的字符串
            /*String data = "exampleData";
            String secretKey = "yourSecretKey";*/

            // 拼接字符串
//            String toHash = data + secretKey;

            // 获取SHA-256 MessageDigest 实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");

            // 对字符串进行哈希计算
            byte[] hashBytes = digest.digest(toHash.getBytes());

            // 将字节数组转换为16进制表示的字符串（小写）
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            // 输出X-Sign值
            String xSign = hexString.toString();
            return xSign;
//            System.out.println("X-Sign: " + xSign);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


}
